import threading
import time

flag_thread_stop = False


def thread_task(id):
    cur_thread = threading.current_thread()
    while flag_thread_stop != True:
        print(
            f"{id}线程 name:{cur_thread.name}, id:{cur_thread.ident}, native_id:{threading.get_native_id()}, main_thread:{threading.main_thread().ident}"
        )
        time.sleep(2)

    print(f"{id}线程结束")


def cmd():
    help = """
1. create 线程 2.销毁线程
3. 获取线程列表
5. 获取线程状态
q 退出
"""
    print(help)

    global flag_thread_stop
    task_id = []
    count = 0

    while True:
        cmd = input()
        if cmd == "1":
            task = threading.Thread(target=thread_task, args=[count])
            task_id.append(task)
            task.start()
            count += 1

        elif cmd == "2":
            flag_thread_stop = True
            for task in task_id:
                task.join()
                task_id.remove(task)
            print("销毁全部线程")

        elif cmd == "3":
            thread_list = threading.enumerate()
            for thread in thread_list:
                print(
                    f"遍历线程 name:{thread.name}, id:{thread.ident}, native_id:{threading.get_native_id()}, main_thread:{threading.main_thread().ident}"
                )

        elif cmd == "5":
            active_count = threading.active_count()
            id = threading.get_ident()
            print(
                f"线程数量：{active_count}, 线程id {id}, native_id:{threading.get_native_id()}, main_thread:{threading.main_thread().ident}"
            )
        elif cmd == "q":
            break
        else:
            print(help)


if __name__ == "__main__":
    cmd()
